/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.ObjectBean.RequestBean.*;
import Model.BeanUltility.*;
import Model.DataUltility.*;
import Controller.CoreServlet.Ultility.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
/**
 *
 * @author chickencake1
 */
public class registerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Init -----------
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Data_Access access = new Data_Access();
        BeanRegister register = new BeanRegister();
        RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/MainJSP/RegisterPage.jsp");
        PopulateBean.populateBeans(register, request); 
        
        response.setContentType("text/html;charset=UTF-8");
        confirm conf =(confirm) request.getSession().getAttribute("conf");
        register.setStatus("false");
        ArrayList problemlist = new ArrayList();
        
        out.println(Register.checkMissingBoolean(request));
        if(!Register.checkMissingBoolean(request))
        {
            register.setStatus("true");
            register.setFieldMissing(Register.checkMissing(request));
            problemlist.add("Một vài mục dữ liệu bạn quên cung cấp cho chúng tôi!");
        }
               
        
        if(!conf.getConfirmcode().equalsIgnoreCase(request.getParameter("confirm_code")))          
        {
            register.setStatus("true");
            register.setCaptcharStatus(false);
            problemlist.add("Mã xác nhận nhập không chính xác , hãy thử lại !");
        }
        
        if(Register.checkUsername(request))
        {
            register.setStatus("true");
            problemlist.add("Tên account đã được đăng kí , hãy thử lại !");
        }
        
        if(register.getStatus().equals("false"))
        {
        
        // populate inform to Bean from client 
                     
       // Push down DB
       String birthday=register.getYear()+"/"+register.getMonth()+"/"+register.getDay();
       register.setBirthday(birthday);
       String []account_data={register.getUsername(),register.getPassword()};
       String []personal_data={register.getUsername(),register.getFirstname(),register.getLastname(),
       register.getEmail(),register.getAddress(),register.getSex(),register.getPhonenumber(),
       register.getCity(),register.getBirthday(),register.getPayment_style(),register.getQuestion(),
       register.getAnswer()};
       

        access.insertInformation("login_account", account_data);
        access.insertInformation("personal_info", personal_data);
        
        if(request.getParameter("payment_style").equals("1"))
        {
            String [] paymentData={register.getUsername()
                    ,register.getAccount_ID(),register.getPostal_code()};
            
            access.insertInformation("payment_account", paymentData);
        }
        
        request.getSession().setAttribute("register",register);
        dispatcher.forward(request, response);
        
        for(int i=0;i<personal_data.length;i++)
       {
           out.println(personal_data[i]);
       }
        out.println("Confirm code là" + conf.getConfirmcode());
        }
        else
        {
            register.setProblemList(problemlist);
            request.getSession().setAttribute("register",register);
            dispatcher.forward(request, response);
           /* for(int i=0;i<problemlist.size();i++)
            {
                out.println(problemlist.get(i));
            }
            out.println(conf.getConfirmcode());
            out.println(request.getParameter("confirm_code"));*/
            
            ArrayList a = register.getProblemList();
            for(int i=0;i<a.size();i++)
            {
                out.println(a.get(i));
            }
            
           

            for(int i=0;i<register.getFieldMissing().size();i++)
                out.println(i + "] " + register.getFieldMissing().get(i));
        }
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    

}
